
package com.kweative.format;
import com.kweative.type.Node;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class UrlFormat extends Format {
	@Override
	public Node parse(InputStream in) {
        BufferedReader br = null;
        String string = "";
        try {
            StringBuilder sb = new StringBuilder();
            br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            br.close();
            string = sb.toString();
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(UrlFormat.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(UrlFormat.class.getName()).log(Level.SEVERE, null, ex);
        }
        Node root = new Node("container");
        String[] nameVals = string.split("&");
        for(int i = 0; i < nameVals.length;i++) {
            String[] parts = nameVals[i].split("=",2);
            if (parts.length == 2)
                root.makeChild(parts[0]).setValue(parts[1].trim());
        }
        return root;
	}

	@Override
	public void serialize(Node node, OutputStream xmlout) throws Exception {
		
	}


}
